package com.googlecode.jascii.printing;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;

import org.apache.commons.io.FileUtils;

public class TextReportPrinter implements Printable {
	private File txtFile;
	private String[] pageTxt = null;
	private int ipageStart = 0;
	private int ipageEnd = 1;
	private float fontSize;
	private String encode = null;
	public TextReportPrinter(File txtFile  , int fontSize , String encode) {
		super();
		this.txtFile = txtFile;
		this.fontSize = fontSize;
		this.encode = encode;
	}
	public TextReportPrinter(File txtFile  , int fontSize ) {
		super();
		this.txtFile = txtFile;
		this.fontSize = fontSize;
	}
	public TextReportPrinter(String txtContent  , int fontSize ) {
		super();
		this.txtFile = null;
		this.fontSize = fontSize;
		pageTxt = txtContent.split("\f");
	}
	public TextReportPrinter(String txtContent  , int fontSize , String encode) {
		super();
		this.txtFile = null;
		this.fontSize = fontSize;
		pageTxt = txtContent.split("\f");
		this.encode = encode;
	}
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
			throws PrinterException {
		
		Graphics2D g2 = (Graphics2D) graphics; 
		g2.setPaint(Color.black); 
		if(pageIndex > pageTxt.length)
			return Printable.NO_SUCH_PAGE;
		if(ipageStart > ipageEnd) return Printable.NO_SUCH_PAGE;
		System.out.println("pageIndex="+ipageStart);
		g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
		//Font font = new Font("Serif", Font.PLAIN, fontSize);
		Font fireflyFontDynamic = FontUtils.getFont("fireflysung.ttf");
		Font fireflyFont = fireflyFontDynamic.deriveFont(fontSize  );
		//Font font = new Font("細明體", Font.PLAIN, fontSize);//設置字型 

		g2.setFont(fireflyFont); 
		
		String page = pageTxt[ipageStart++];
		String[] lineTxt = page.split("\n");
		
		int ix=0;
		float y = fontSize;
		for (int i = 0; i < lineTxt.length; i++) {
		      String line = (String)lineTxt[i];
		      for(int itemp=0;itemp<line.length();itemp++)
		      {
		    	  char ich = line.charAt(itemp);
		    	  String istr = String.valueOf(ich);
		    	  
		    	  if(istr.getBytes().length==1)
		    	  {
		    		  g2.drawString(String.valueOf(line.charAt(itemp)), (int)ix, (int)y);
		    	      ix+=fontSize/2;
		    	  }
		    	  else
		    	  {
		    		  g2.drawString(String.valueOf(line.charAt(itemp)), (int)ix, (int)y);
		    		  ix+=fontSize;
		    	  }
		    		  
		      }
		      y += fontSize;
		      ix=0;
		    }
		
		return Printable.PAGE_EXISTS; 
	}
	public void printPage() throws PrinterNotFoundException, IOException
	{
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
		pras.add(MediaSizeName.ISO_A4); 
		pras.add(new Copies(1));
		printPage(null,pras);
	}
	public void printPage(String prnName , int pageStart,int pageEnd) throws PrinterNotFoundException, IOException
	{
		this.ipageStart = pageStart;
		this.ipageEnd = pageEnd;
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
		pras.add(MediaSizeName.ISO_A4); 
		pras.add(new Copies(1));
		printPage(prnName,pras);
	}
	public void printPage( String prnName , int pageStart,int pageEnd,PrintRequestAttributeSet pras) throws PrinterNotFoundException, IOException
	{
		this.ipageStart = pageStart;
		this.ipageEnd = pageEnd;
		printPage(null,pras);
	}
	public void printPage( int pageStart,int pageEnd) throws PrinterNotFoundException, IOException
	{
		this.ipageStart = pageStart;
		this.ipageEnd = pageEnd;
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
		pras.add(MediaSizeName.ISO_A4); 
		pras.add(new Copies(1));
		printPage(null,pras);
	}
	public void printPage( int pageStart,int pageEnd,PrintRequestAttributeSet pras) throws PrinterNotFoundException, IOException
	{
		this.ipageStart = pageStart;
		this.ipageEnd = pageEnd;
		printPage(null,pras);
	}
	public void printPage(String prnName ) throws PrinterNotFoundException, IOException
	{
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
		pras.add(MediaSizeName.ISO_A4); 
		pras.add(new Copies(1));
		printPage(prnName,pras);
	}
	public void printPage(String prnName ,PrintRequestAttributeSet pras) throws PrinterNotFoundException, IOException
	{
		if(pageTxt ==null)
		{
			String reportTxt;
			if(encode==null)
			  reportTxt=FileUtils.readFileToString(txtFile);
			else  reportTxt=FileUtils.readFileToString(txtFile,encode);
			pageTxt = reportTxt.split("\f");
		}
		DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; 
		PrintService printService  =  FoundPrinter.getNamedPrintService(prnName) ; 
		DocPrintJob job = printService.createPrintJob(); 
		
//		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
//		pras.add(MediaSizeName.ISO_A4); 
//		pras.add(new Copies(copy));
		DocAttributeSet das = new HashDocAttributeSet(); 
		
		
		Doc doc = new SimpleDoc(this, flavor, das); 
		try { 
				job.print(doc, pras);
		} catch (PrintException pe) { 
			pe.printStackTrace(); 
		} 
		
	}
	
	

}
